package com.ruoyi.teaching.controller;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.teaching.dto.AttendanceDTO;
import com.ruoyi.teaching.vo.AttendanceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.teaching.domain.YaAttendance;
import com.ruoyi.teaching.service.IYaAttendanceService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 考勤汇总Controller
 *
 * @author ruoyi
 * @date 2025-03-31
 */
@RestController
@RequestMapping("/attendance")
public class YaAttendanceController extends BaseController
{
    @Autowired
    private IYaAttendanceService yaAttendanceService;

    /**
     * 查询考勤汇总列表
     */
    @RequiresPermissions("teaching:attendance:list")
    @GetMapping("/list")
    public TableDataInfo list(YaAttendance yaAttendance)
    {
        startPage();
        List<YaAttendance> list = yaAttendanceService.selectYaAttendanceList(yaAttendance);
        return getDataTable(list);
    }

    /**
     * 查询考勤汇总列表
     */
    @RequiresPermissions("teaching:attendance:list")
    @GetMapping("/listAll")
    public TableDataInfo listAll(Date date)
    {
        List<AttendanceDTO> list = yaAttendanceService.selectYaAttendanceListByClassName(date);
        return getDataTable(list);
    }

    /**
     * 查询每天考勤汇总列表
     */
    @RequiresPermissions("teaching:attendance:list")
    @GetMapping("/listStuOrAll")
    public TableDataInfo listStuOrAll(Date date) throws ParseException {
        List<AttendanceVo> list = yaAttendanceService.selectYaAttendanceStuOrAllListByDate(date);
        return getDataTable(list);
    }

    /**
     * 导出考勤汇总列表
     */
    @RequiresPermissions("teaching:attendance:export")
    @Log(title = "考勤汇总", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YaAttendance yaAttendance)
    {
        List<YaAttendance> list = yaAttendanceService.selectYaAttendanceList(yaAttendance);
        ExcelUtil<YaAttendance> util = new ExcelUtil<YaAttendance>(YaAttendance.class);
        util.exportExcel(response, list, "考勤汇总数据");
    }

    /**
     * 获取考勤汇总详细信息
     */
    @RequiresPermissions("teaching:attendance:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(yaAttendanceService.selectYaAttendanceById(id));
    }

    /**
     * 新增考勤汇总
     */
    @RequiresPermissions("teaching:attendance:add")
    @Log(title = "考勤汇总", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YaAttendance yaAttendance)
    {
        return toAjax(yaAttendanceService.insertYaAttendance(yaAttendance));
    }

    /**
     * 修改考勤汇总
     */
    @RequiresPermissions("teaching:attendance:edit")
    @Log(title = "考勤汇总", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YaAttendance yaAttendance)
    {
        return toAjax(yaAttendanceService.updateYaAttendance(yaAttendance));
    }

    /**
     * 删除考勤汇总
     */
    @RequiresPermissions("teaching:attendance:remove")
    @Log(title = "考勤汇总", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(yaAttendanceService.deleteYaAttendanceByIds(ids));
    }

    /**
     * 批量更新学生考勤信息
     */
    @RequiresPermissions("teaching:attendance:edit")
    @Log(title = "学生考勤", businessType = BusinessType.UPDATE)
    @PutMapping("/updateStudentAttendance")
    public AjaxResult updateStudentAttendance(@RequestBody List<YaAttendance> attendanceList)
    {
        return toAjax(yaAttendanceService.updateBatchAttendance(attendanceList));
    }
}
